package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.CartMapper;
import com.mapper.GoodsMapper;
import com.mapper.OrderGoodsMapper;
import com.mapper.OrderMapper;
import com.pojo.bo.GoodsBo;
import com.pojo.dto.OrderDto;
import com.pojo.dto.OrderGoodsDto;
import com.pojo.dto.OrdersDto;
import com.pojo.entity.Orders;
import com.pojo.entity.OrderGoods;
import com.service.IOrderGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : 杨世博
 * @date: Created in 15:49 2023/6/4
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements IOrderGoodsService {

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CartMapper cartMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public OrderDto submitOrder(OrderGoodsDto orderGoodsDto) {

        Orders orders = new Orders();
        orders.setUid(orderGoodsDto.getUid());
        orders.setGoodsMoney(orderGoodsDto.getGoodsMoney());
        orders.setNeedPay(orderGoodsDto.getGoodsMoney());
        orders.setUserAddress(orderGoodsDto.getUserAddress());
        orders.setOrderState(-2);
        orders.setMerchantId(orderGoodsDto.getMerchantId());

        orderMapper.insertOrder(orders);

        List<GoodsBo> goods = orderGoodsDto.getGoods();
        for (GoodsBo good : goods) {
            OrderGoods orderGoods = new OrderGoods();
            orderGoods.setGoodsId(good.getGoodId());
            orderGoods.setGoodsName(good.getGoodsTitle());
            orderGoods.setGoodsImg(good.getGoodsImage());
            orderGoods.setGoodsPrice(good.getSumPrice());
            orderGoods.setGoodsNum(good.getCartNum());
            orderGoods.setOrderId(orders.getOrderId());
            orderGoods.setGoodsState(0);

            orderGoodsMapper.insert(orderGoods);
        }

        QueryWrapper<OrderGoods> oqw = new QueryWrapper<>();
        oqw.eq("order_id", orders.getOrderId());
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(oqw);

        OrderDto orderDto = new OrderDto();
        orderDto.setAllPrice(orders.getGoodsMoney());
        orderDto.setGoods(orderGoods);

        return orderDto;
    }

    @Override
    public Boolean deleteOrder(Integer id) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("order_id", id);
        orderMapper.delete(oqw);

        QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
        ogqw.eq("order_id", id);
        orderGoodsMapper.delete(ogqw);

        return true;
    }

    @Override
    public List<OrdersDto> getLogistics(Integer uid) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("uid", uid).eq("order_state", 2);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> orderDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto orderDto = new OrdersDto();
            BeanUtils.copyProperties(order, orderDto);
            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            orderDto.setOrderGoods(orderGoods);

            orderDtoList.add(orderDto);
        }

        return orderDtoList;
    }

    @Override
    public Boolean refund(Integer orderId) {
        orderMapper.refund(orderId);
        orderGoodsMapper.refund(orderId);
        return true;
    }

    @Override
    public Boolean confirmReceipt(Integer orderId) {
        Date date = new Date();
        orderMapper.confirmReceipt(orderId,date);
        return true;
    }

    @Override
    public List<OrdersDto> findPersonAllOrder(Integer uid) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("uid", uid);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);

            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            ordersDto.setOrderGoods(orderGoods);
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }

    @Override
    public List<OrdersDto> findPersonNotPaidOrder(Integer uid) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("uid", uid).eq("order_state", -2);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);

            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            ordersDto.setOrderGoods(orderGoods);
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }

    @Override
    public List<OrdersDto> findPersonPaidOrder(Integer uid) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("uid", uid).ne("order_state", -2).ne("order_state",2);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);

            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            ordersDto.setOrderGoods(orderGoods);
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }

    @Override
    public List<OrdersDto> findShopAllOrder(Integer merchantId) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("merchant_id", merchantId);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);

            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            ordersDto.setOrderGoods(orderGoods);
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }

    @Override
    public List<OrdersDto> findShopNotPaidOrder(Integer merchantId) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("merchant_id", merchantId).eq("order_state", -2);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);

            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            ordersDto.setOrderGoods(orderGoods);
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }

    @Override
    public List<OrdersDto> findShopRefundOrder(Integer merchantId) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("merchant_id", merchantId);
        oqw.eq("order_state", 3).or().eq("order_state",-4);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);

            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            ordersDto.setOrderGoods(orderGoods);
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }

    @Override
    public void payOrder(Integer orderId) {
        orderMapper.updateOrder(orderId);
    }

    @Override
    public void dealWithRefund(Integer orderId) {
        orderMapper.dealWithRefund(orderId);
        orderGoodsMapper.dealWithRefund(orderId);
    }

    @Override
    public void deliverGoods(Integer expressNo, String expressName, Integer orderId) {
        orderMapper.deliverGoods(expressNo, expressName, orderId);
    }

    @Override
    public List<OrdersDto> getShopDeliverOrder(Integer merchantId) {
        QueryWrapper<Orders> oqw = new QueryWrapper<>();
        oqw.eq("merchant_id", merchantId);
        oqw.eq("order_state", 1).or().eq("order_state",2).eq("order_state",4);
        List<Orders> orders = orderMapper.selectList(oqw);

        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order,ordersDto);

            QueryWrapper<OrderGoods> ogqw = new QueryWrapper<>();
            ogqw.eq("order_id",order.getOrderId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectList(ogqw);
            ordersDto.setOrderGoods(orderGoods);
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }
}
